package com.aio.kgo.simple.form.design.controller;

import cn.hutool.extra.spring.SpringUtil;
import com.aio.kgo.simple.form.common.enums.DefaultElementEnum;
import com.aio.kgo.simple.form.common.enums.InputFormatEnum;
import com.aio.kgo.simple.form.common.enums.ModelLayoutEnum;
import com.aio.kgo.simple.form.design.def.AbstractModelModuleDef;
import com.kgo.flow.common.domain.amis.AmisOptions;
import com.kgo.flow.common.domain.amis.AmisResult;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lizhenming
 * @desc:
 * @date 2023/12/25 22:02
 */
@RestController
@RequestMapping("/simple/model/enum")
public class ModelEnumController {
    /**
     * 获取模型布局枚举
     * @Param
     * @return
     * @Author: lizhenming
     * @Date:
     **/
    @GetMapping("layout/options")
    public AmisResult getModelLayoutOptions(){
        AmisOptions options = new AmisOptions();
        for (ModelLayoutEnum layoutItem : ModelLayoutEnum.values()) {
            if (ModelLayoutEnum.VERTICAL.equals(layoutItem)){
                options.addOption(layoutItem.getDesc(),layoutItem.getLayout(),true);
            }else {
                options.addOption(layoutItem.getDesc(),layoutItem.getLayout(),false);
            }
        }
        return AmisResult.success(options);
    }
    @GetMapping("default/element/options")
    public AmisResult getDefaultElementOptions(){
        AmisOptions options = new AmisOptions();
        for (DefaultElementEnum elementEnum : DefaultElementEnum.values()) {
            if (DefaultElementEnum.INPUT.equals(elementEnum)){
                options.addOption(elementEnum.getDesc(),elementEnum.getElement(),true);
            }else {
                options.addOption(elementEnum.getDesc(),elementEnum.getElement(),false);
            }
        }
        return AmisResult.success(options);
    }
    @GetMapping("input/type/options")
    public AmisResult getInputTypeOptions(){
        AmisOptions options = new AmisOptions();
        for (InputFormatEnum formatEnum : InputFormatEnum.values()) {
            if (InputFormatEnum.TEXT.equals(formatEnum)){
                options.addOption(formatEnum.getDesc(),formatEnum.getFormat(),true);
            }else {
                options.addOption(formatEnum.getDesc(),formatEnum.getFormat(),false);
            }
        }
        return AmisResult.success(options);
    }

    @GetMapping("module/options")
    public AmisResult getModelModuleOptions(){
        AmisOptions options = new AmisOptions();
        String[] moduleBeans = SpringUtil.getBeanNamesForType(AbstractModelModuleDef.class);
        if (ObjectUtils.isEmpty(moduleBeans)){
            return AmisResult.success(options);
        }
        for (String moduleBean : moduleBeans) {
            AbstractModelModuleDef bean = SpringUtil.getBean(moduleBean, AbstractModelModuleDef.class);
            options.addOption(bean.getModuleName(),bean.getModuleKey());
        }
        return AmisResult.success(options);
    }
}
